package com.hc.server.actions;

import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.hc.server.Error;
import com.hc.server.IAction;
import com.hc.server.Request;
import com.hc.server.Response;
import com.hc.server.conn.mall.MallResponse;
import com.hc.server.conn.mall.MallServerHelper;

public class QUERY_TICKETS implements IAction {

	private final static String CITY = "CITY";
	private final static String CHECKIN_DATE = "CHECKIN_DATE";
	private final static String NAME = "NAME";
	private final static String KEY = "KEY";
	private final static String SORT = "SORT";
	private final static String PAGE_INDEX = "PAGE_INDEX";
	private final static String PAGE_SIZE = "PAGE_SIZE";
	
	private static final String SN = "SN";
//	private final static String ADD = "ADD";
	private final static String PIC = "PIC";
//	private final static String FEE = "FEE";//费用包含
//	private final static String USAGE = "USAGE";//如何使用
//	private final static String NOTES = "NOTES";//购买须知
	private final static String MPRICE = "MPRICE";	
	private final static String SPRICE = "SPRICE";
//	private final static String DES = "DES";
	private final static String STOCK = "STOCK";
	
	private final static String TICKETS = "TICKETS";
	private final static String TOTAL_RESULT = "TOTAL_RESULT";
	
	//MALL字段
	private final static String MALL_TOTAL_COUNT = "totalCount";
//	private final static String MALL_ = "rowsOfPage";
//	private final static String MALL_ = "totalPage";
//	private final static String MALL_ = "currentPage";
	private final static String MALL_LIST = "list";

	private final static String MALL_SN = "pdtSn";
	private final static String MALL_NAME = "name";
//	private final static String MALL_ADDRESS = "address";
	private final static String	MALL_PICURI = "picUri";
	//private final static String MALL_PHONE = "phone";
//	private final static String MALL_DES = "brief";
//	private final static String MALL_USAGE = "useExplain";
//	private final static String MALL_FEE = "feeExplain";
//	private final static String MALL_NOTES = "bookExplain";
	//private final static String MALL_PROMPT = "pdtExplain";
	private static final String MAll_MARKETPRICE = "marketPrice";
	private static final String MALL_SALEPRICE = "localPrice";
	private static final String MALL_TYPE = "ticketType";
	private static final String MALL_LV = "level";
	private static final String MALL_DATE = "saleDate";
	private static final String MALL_STOCK = "stockCount";


	
	@Override
	public void execute(Request req, Response resp) {
		//1.处理参数
		String city;
		try{
			city = req.getStrArg(CITY);
		}catch (Exception e) {
			resp.setErrResp(Error.INVALID_ARGS_ERR_CD, Error.INVALID_ARGS_ERR_MSG);
			return;
		}
		//可选参数
		String checkInDate = req.optStrArg(CHECKIN_DATE);
		String name = req.optStrArg(NAME);
		String key = req.optStrArg(KEY);
		int orderBy = req.optIntArg(SORT);
		int pageIndex = req.optIntArg(PAGE_INDEX);
		int pageSize = req.optIntArg(PAGE_SIZE);
		
		//2.查询
		MallResponse mResp = null;
		
		try{
			mResp = MallServerHelper.queryTickets(city, checkInDate, name, key, orderBy, pageIndex, pageSize);
		}catch (IOException e) {
			e.printStackTrace();
			resp.setErrResp(Error.MALLSERVER_NET_ERR_CD, Error.MALLSERVER_NET_ERR_MSG);
			return;
		} catch (JSONException e) {
			e.printStackTrace();
			resp.setErrResp(Error.MALLSERVER_DATA_FORMAT_ERR_CD, Error.MALLSERVER_DATA_FORMAT_ERR_MSG);
			return;
		}
		if(0 != mResp.getRetCode()){
			resp.setErrResp(Error.MALLSERVER_ERR_CD, mResp.getErrorMsg());
			return;
		}
		
		//3.组织返回数据
		try{
			JSONObject result = mResp.getResult();
			JSONArray tickets = new JSONArray();
			JSONArray list = result.getJSONArray(MALL_LIST);//应该使用get方式
			if(0 != list.length()){
				for(int index = 0; index < list.length(); index++){
					JSONObject ticket = new JSONObject();
					JSONObject mTicket = list.getJSONObject(index);
					String tPdtSn= mTicket.getString(MALL_SN);
//					MallResponse mTicketResp = null;
//					try{
//						mTicketResp = MallServerHelper.queryTicket(tPdtSn);
//					}catch (Exception e) {
//						resp.setErrResp(Error.SERVER_SYS_ERR_CD, Error.SERVER_SYS_ERR_MSG);
//						return;
//					}
//					if(0 != mTicketResp.getRetCode()){
//						resp.setErrResp(Error.MALL_SERVER_ERR_CD, mResp.getErrorMsg());
//						return;
//					}
//					mTicket = mTicketResp.getResult();
					ticket.put(SN, tPdtSn);
					String tName = mTicket.getString(MALL_NAME);
					ticket.put(NAME, tName);
					//String tAddress = mTicket.getString(MALL_ADDRESS);
					//ticket.put(ADD, tAddress);
					String tPicUrl = mTicket.getString(MALL_PICURI);
					ticket.put(PIC, tPicUrl);
					ticket.put(KEY, "");
					//String tFee = mTicket.getString(MALL_FEE);
					//ticket.put(FEE, tFee);
					//String tUsage = mTicket.getString(MALL_USAGE);
					//ticket.put(USAGE, tUsage);
					//String tNotes = mTicket.getString(MALL_NOTES);
					//ticket.put(NOTES, tNotes);
					int tMarketPrice = mTicket.getInt(MAll_MARKETPRICE);
					ticket.put(MPRICE, tMarketPrice);
					int tSalePrice = mTicket.getInt(MALL_SALEPRICE);
					ticket.put(SPRICE, tSalePrice);
					int type = mTicket.getInt(MALL_TYPE);
					int lv = mTicket.getInt(MALL_LV);
					String date = mTicket.getString(MALL_DATE);
					int stock = mTicket.getInt(MALL_STOCK);
					ticket.put(STOCK, stock);
					tickets.put(ticket);
				}
			}
			resp.appendRespContent(TICKETS, tickets);
			resp.appendRespContent(TOTAL_RESULT, result.getInt(MALL_TOTAL_COUNT));
		}catch (JSONException e) {
			e.printStackTrace();
			resp.setErrResp(Error.SERVER_SYS_ERR_CD, Error.SERVER_SYS_ERR_MSG);
			return;
		}
	}

}
